define(["jquery", "easy-admin"], function ($, ea) {
    var form = layui.form,table = layui.table;
    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'crm.status/index',
        add_url: 'crm.status/add',
        edit_url: 'crm.status/edit',
        delete_url: 'crm.status/delete',
        export_url: 'crm.status/export',
        modify_url: 'crm.status/modify',
    };

    var Controller = {

        adminList: function () {
            var tableIn = ea.table.render({
                elem: '#list',
                url: 'auth/adminList',
                method:'post',
                toolbar: '#topBtn',
                title:'管理员列表',
                cols: [[
                    {field:'admin_id', title: '编号', width:60,fixed: true}
                    ,{field:'username', title: '用户名', width:120}
                    ,{field:'realname', title: '真实姓名', width:120}
                    ,{field:'title', title: '用户组', width:160}
                    ,{field:'standing', title: '身份', width:160}
                    // ,{field:'email', title: '邮箱', width:200}
                    ,{field: 'mubiao', title: '业绩月目标', width: 200}
                    ,{field: 'ticheng', title: '提成点%', width: 100}
                    ,{field:'tel', title: '电话', width:150}
                    ,{field:'ip', title: 'IP',width:150,hide:true}
                    ,{field:'is_open', title: '状态',width:150,toolbar: '#open'}
                    ,{field:'is_poll', title: '是否轮询',width:150,toolbar: '#is_poll'}
                    ,{field:'isphone', title: '手机号查看',width:150,toolbar: '#lookiphone',hide:false}
                    ,{title: '操作',width:260, align:'center', toolbar: '#barDemo'}
                ]]
            });
            form.on('switch(open)', function(obj){
                loading =layer.load(1, {shade: [0.1,'#fff']});
                var id = this.value;
                var is_open = obj.elem.checked===true?1:0;
                $.post(ea.url('auth/adminState'),{'id':id,'is_open':is_open},function (res) {
                    layer.close(loading);
                    if (res.status==1) {
                        tableIn.reload();
                    }else{
                        layer.msg(res.msg,{time:2000,icon:2});
                        return false;
                    }
                })
            });
            form.on('switch(lookiphone)', function(obj){
                loading =layer.load(1, {shade: [0.1,'#fff']});
                var id = this.value;
                var isphone = obj.elem.checked===true?1:0;
                $.post(ea.url('auth/adminPhone'),{'id':id,'isphone':isphone},function (res) {
                    layer.close(loading);
                    if (res.status==1) {
                    }else{
                        layer.msg(res.msg,{time:2000,icon:2});
                        return false;
                    }
                })
            });
            form.on('switch(is_poll)', function(obj){
                loading =layer.load(1, {shade: [0.1,'#fff']});
                var id = this.value;
                var is_poll = obj.elem.checked===true?1:0;
                $.post(ea.url('auth/adminPoll'),{'id':id,'is_poll':is_poll},function (res) {
                    layer.close(loading);
                    if (res.status==1) {
                    }else{
                        layer.msg(res.msg,{time:2000,icon:2});
                        return false;
                    }
                })
            });
            table.on('tool(list)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    layer.confirm('确定要删除当前用户吗', function(index){
                        $.post("auth/adminDel",{admin_id:data.admin_id},function(res){
                            if(res.code==1){
                                layer.msg(res.msg,{time:2000,icon:1});
                                obj.del();
                            }else{
                                layer.msg(res.msg,{time:2000,icon:2});
                            }
                        });
                        layer.close(index);
                    });
                }
            });

            ea.listen();
        },
        adminRule: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
    };
    return Controller;
});
